package com.example.spring_java_oj.controller;

import com.example.spring_java_oj.model.Result;
import com.example.spring_java_oj.service.userInfo.UserInfoService;
import com.example.spring_java_oj.service.UserLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    private UserInfoService userInfoService;

    //登录
    @RequestMapping("/sub")
    public Result login(String username, String password, String captcha, HttpServletRequest request){
        String ret = userLoginService.login(username, password, captcha, request);
        if(ret == null){
            return Result.success("登录成功");
        }
        return Result.fail(ret);
    }

    //注册
    @RequestMapping("/register")
    public boolean register(String username, String passwordA, String passwordB){
        log.info("username: " + username + " password: " + passwordA);
        return userInfoService.regster(username, passwordA, passwordB);
    }

    //获取验证码
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response){
        userLoginService.buildCheckImage(request,response);
    }

}
